package com.jacky.javase;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.commons.dbcp2.BasicDataSourceFactory;
import org.junit.Test;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;

public class ConnectionTests2 {

    @Test
    public void testC3P0ByProperties() throws Exception {
        ComboPooledDataSource cpds = new ComboPooledDataSource("HelloC3P0");
        Connection conn = cpds.getConnection();
        System.out.println(conn);
    }

    @Test
    public void testDBCPByProperties() throws Exception {
        Properties properties = new Properties();
        InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("properties/druid.properties");
        properties.load(is);

        BasicDataSource source = BasicDataSourceFactory.createDataSource(properties);
        Connection conn = source.getConnection();
        System.out.println(conn);
    }

    @Test
    public void testDruidByProperties() throws Exception {
        Properties pros = new Properties();
        InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("properties/druid.properties");
        pros.load(is);

        DataSource source = DruidDataSourceFactory.createDataSource(pros);
        Connection conn = source.getConnection();
        System.out.println(conn);

    }
}
